package com.mlkj.ytdc.modules;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.mlkj.ytdc.utils.Event;

/**
 * 设备亮度管理
 */
public class LuminanceManage extends ReactContextBaseJavaModule {

    @NonNull
    @Override
    public String getName() {
        return "LuminanceManage";
    }

    public LuminanceManage(@NonNull ReactApplicationContext reactContext) {
        super(reactContext);
    }

    /**
     * 获取最大系统亮度(一般是 255)
     */
    public int getMaxLuminance() {
        try {
            int brightnessSettingMaximumId = getReactApplicationContext().getResources().getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android");
            int brightnessSettingMaximum = getReactApplicationContext().getResources().getInteger(brightnessSettingMaximumId);
            return brightnessSettingMaximum;
        } catch (RuntimeException e) {
            Event.logEmit("获取最大亮度报错: " + e.getMessage());
            return 255;
        }
    }

    /**
     * 获取最小系统亮度
     */
    public int getMinLuminance() {
        try {
            int brightnessSettingMinimumId = getReactApplicationContext().getResources().getIdentifier("config_screenBrightnessSettingMinimum", "integer", "android");
            int brightnessSettingMinimum = getReactApplicationContext().getResources().getInteger(brightnessSettingMinimumId);
            return brightnessSettingMinimum;
        } catch (RuntimeException e) {
            Event.logEmit("获取最小亮度报错: " + e.getMessage());
            return 0;
        }
    }

    /**
     * 获取系统亮度
     */
    @ReactMethod
    public void getLuminance(Promise promise) {
        try {
            int anInt = Settings.System.getInt(getReactApplicationContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
            int val = 100 * anInt / getMaxLuminance();
            promise.resolve(val);
        } catch (Settings.SettingNotFoundException e) {
            Event.logEmit("亮度获取失败: " + e.getMessage());
            promise.resolve(null);
        }
    }

    /**
     * 修改亮度
     */
    @ReactMethod
    public void setLuminance(int num, Promise promise) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 需要修改系统设置权限
            if (Settings.System.canWrite(getReactApplicationContext())) {
                try {
                    int a = (int) Math.ceil((num) * getMaxLuminance() * 0.01);
                    a = Math.max(a, 0);
                    a = Math.min(a, 100);
                    Settings.System.putInt(getReactApplicationContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, a);
                    promise.resolve(null);
                } catch (RuntimeException e) {
                    Event.logEmit("亮度修改失败: " + e.getMessage());
                    promise.resolve(e.getMessage());
                }
            } else {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
                        Uri.parse("package:" + getReactApplicationContext().getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Bundle bundle = new Bundle();
                getReactApplicationContext().startActivityForResult(intent, 1, bundle);
                Toast.makeText(getReactApplicationContext(), "请设置修改系统设置权限", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
